﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Catan
{
    class Game
    {
        private List<Tile> _Tiles;

        private Tile _RobberTile;



        public List<Tile> Tiles
        {
            get
            {
                return _Tiles;
            }
        }


        public Tile RobberTile
        {
            get
            {
                return _RobberTile;
            }
        }


        public void Initialize()
        {
            List<byte> probabilityTokens =
                new List<byte>(
                    new byte[]
                        {
                            2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12
                        });

            List<ResourceType> resources =
                new List<ResourceType>(
                    new ResourceType[]
                        {
                            ResourceType.Wood,
                            ResourceType.Wood,
                            ResourceType.Wood,
                            ResourceType.Wood,
                            ResourceType.Sheep,
                            ResourceType.Sheep,
                            ResourceType.Sheep,
                            ResourceType.Sheep,
                            ResourceType.Wheat,
                            ResourceType.Wheat,
                            ResourceType.Wheat,
                            ResourceType.Wheat,
                            ResourceType.Brick,
                            ResourceType.Brick,
                            ResourceType.Brick,
                            ResourceType.Rock,
                            ResourceType.Rock,
                            ResourceType.Rock,
                            ResourceType.Sand
                        });

            Debug.Assert(probabilityTokens.Count + 1 == resources.Count);
            int maxTileCount = resources.Count;

            _Tiles = new List<Tile>();
            for(int tileCount = 0; tileCount < maxTileCount; tileCount += 1)
            {
                int randomIndex = RandomNumberGenerator.Generate(resources.Count - 1);
                ResourceType resource = resources[randomIndex];
                resources.RemoveAt(randomIndex);

                if (resource == ResourceType.Sand)
                {
                    _RobberTile = new Tile(resource, 0);
                    _Tiles.Add(_RobberTile);
                    continue;
                }

                randomIndex = RandomNumberGenerator.Generate(probabilityTokens.Count - 1);
                byte diceRoleRequired = probabilityTokens[randomIndex];
                probabilityTokens.RemoveAt(randomIndex);

                _Tiles.Add(new Tile(resource, diceRoleRequired));
            }

            Debug.Assert(probabilityTokens.Count == resources.Count && resources.Count == 0);
        }
    }
}
